package com.dodayum.service;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class DemoRegistry implements InitializingBean {
    private final List<DemoRegistryService> list;
    private static final Map<String, DemoRegistryService> map = new HashMap<>();
    @Override
    public void afterPropertiesSet() {
        for (DemoRegistryService demoRegistryService : this.list) {
            map.put(demoRegistryService.mode(), demoRegistryService);
        }
    }

    public DemoRegistry(List<DemoRegistryService> list) {
        this.list = list;
        System.out.println("发现的实现类"+list.size());
    }

    public DemoRegistryService getDemoRegistryService(String mode) {
        DemoRegistryService response = map.get(mode);
        if (ObjectUtils.isEmpty(response)) {
            throw new RuntimeException("不对哦");
        }
        return response;
    }
}
